package com.ffms.servlet;

import com.ffms.entity.FamilyMember;
import com.ffms.service.AdminService;
import com.ffms.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/UpdateFMServlet")
public class UpdateFMServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		//1.接收视图层传来的表单数据并封装成为对象
		String name = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		FamilyMember fm= new FamilyMember(name, pwd);
		//2.调用业务层完成修改操作
		AdminService adminService = new AdminServiceImpl();
		int n = adminService.updateFM(fm);
		//3 根据结果跳转到不同页面
		if(n>0){		
			//成功则挑转到主页面
			request.getRequestDispatcher("/ShowAllFMServlet").forward(request, response);
		}else{
			//失败则提示
			request.setAttribute("error", "更新成员失败");
			request.getRequestDispatcher("/PreUpdateFMServlet?name="+name).forward(request, response);//!!!!
		}
	}

}
